package com.ruoyi.web.controller.youtobe;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.youtobe.domain.VideoInfoIO;
import com.ruoyi.youtobe.domain.VideoInfoVO;
import com.ruoyi.youtobe.domain.YoutobeChannelVideo;
import com.ruoyi.youtobe.domain.YoutobeChannelVideoVO;
import com.ruoyi.youtobe.service.YoutubeVideoService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Title:
 * @author: WYJ
 * @date: 2025/11/5
 */
@RestController
@RequestMapping("/youtube/video")
public class YoutubeVideoController extends BaseController {

    @Autowired
    private YoutubeVideoService youtubeVideoService;

    /**
     * @description: 获取数据导出
     * @author: WYJ
     * @date: 2025/11/16
     * @param:
     */
    @PostMapping("/getVideo")
    public void getVideoInfoList(VideoInfoIO params, HttpServletResponse response) {
        try {
            // 获取视频数据
            List<VideoInfoVO> videoList = youtubeVideoService.getVideoInfoList(params);

            // 设置图片尺寸（像素）
            int imageWidth = 320;
            int imageHeight = 180;

            // 创建Excel
            Workbook workbook = VideoExcelExporter.createExcelWithImagesAndText(
                    videoList, imageWidth, imageHeight);

            // 设置响应
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename=video_report.xlsx");
            response.setHeader("Cache-Control", "no-cache");

            // 输出Excel
            workbook.write(response.getOutputStream());
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }


    /**
     * @description: 获取YouToBe主页博主视频，并入库
     * @author: WYJ
     * @date: 2025/11/8
     * @param:
     */
    @PostMapping("/getVideoByChannel")
    public AjaxResult getVideoByChannel(VideoInfoIO params) {
        return youtubeVideoService.getVideoByChannel(params);
    }


    /**
     * @description: 获取视频信息
     * @author: WYJ
     * @date: 2025/11/13
     * @param:
     */
    @GetMapping("/getVideoList")
    public TableDataInfo getVideoList(YoutobeChannelVideo params) {
        startPage();
        List<YoutobeChannelVideoVO> list =   youtubeVideoService.getVideoList(params);
        return getDataTable(list);
    }
    /**
     * @description: 频道信息更新
     * @author: WYJ
     * @date: 2025/11/12
     * @param:
     */
    @PostMapping("/updateChannel")
    public AjaxResult updateChannel() {
        return youtubeVideoService.updateChannel();
    }

    /**
     * @description: 尽三天播放量更新
     * @author: WYJ
     * @date: 2025/11/12
     * @param:
     */
    @PostMapping("/updateViewCount")
    public AjaxResult updateViewCount() {
        return youtubeVideoService.updateViewCount();
    }

    //图片下载
    @PostMapping("/downloadPic")
    public AjaxResult downloadPicByUrl(Integer num) {
        return youtubeVideoService.downloadPicByUrl(num);
    }

    /**
     * @description: 我的频道下视频更新
     * @author: WYJ
     * @date: 2025/11/25
     * @param:
     */
    @PostMapping("/updateMyChannelVideo")
    public AjaxResult updateMyChannelVideo() {
        return youtubeVideoService.updateMyChannelVideo();
    }

}